package com.sunwayworld.basemodule.coredata.pjmethod.service.impl;

import com.sunwayworld.basemodule.coredata.pjmethod.bean.LimsPjMethodVersionBean;
import com.sunwayworld.basemodule.coredata.pjmethod.service.LimsPjMethodVersionService;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.basemodule.coredata.pjmethod.dao.LimsPjMethodDao;
import com.sunwayworld.basemodule.coredata.pjmethod.bean.LimsPjMethodBean;
import com.sunwayworld.basemodule.coredata.pjmethod.service.LimsPjMethodService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsPjMethodServiceImpl implements LimsPjMethodService {

    @Autowired
    private LimsPjMethodDao limsPjMethodDao;

    @Autowired
    @Lazy
    private LimsPjMethodVersionService methodVersionService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsPjMethodDao getDao() {
        return limsPjMethodDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsPjMethodBean limsMethod = jsonWrapper.parseUnique(LimsPjMethodBean.class);
        limsMethod.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsMethod);
        // sop版本表加数据
        LimsPjMethodVersionBean methodVersionBean = new LimsPjMethodVersionBean();
        methodVersionBean.setId(ApplicationContextHelper.getNextSequenceNumber(methodVersionService.getDao().getTable()));
        methodVersionBean.setMethodCode(limsMethod.getMethodCode());
        methodVersionBean.setMethodId(limsMethod.getId());
        methodVersionService.getDao().insert(methodVersionBean);
        return limsMethod.getId();
    }

    @Transactional
    @Override
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsPjMethodBean> items = wrapper.parse(LimsPjMethodBean.class);
        if(CollectionUtils.isEmpty(items)) return;
        List<LimsPjMethodVersionBean> methodVersionBeans = methodVersionService.selectListByFilter(SearchFilter.instance()
            .match("methodId", items.stream().map(LimsPjMethodBean::getId).collect(Collectors.toList())).filter(MatchPattern.OR));
        methodVersionService.getDao().deleteBy(methodVersionBeans);
        this.getDao().deleteBy(items);
    }

}
